<template>
  <el-card>
    <el-button
      type="primary"
      :icon="Plus"
      :disabled="!categoryStore.category3Id"
      @click="showSaveOrUpdate"
      >添加Spu</el-button
    >
    <el-table border :data="spuList" class="spu-table">
      <el-table-column label="序号" type="index" align="center" width="60" />
      <el-table-column label="spu名称" prop="spuName" />
      <el-table-column label="spu描述" prop="description" />

      <el-table-column label="操作" width="250">
        <template v-slot="{ row }">
          <el-tooltip content="添加" placement="bottom" effect="light">
            <el-button
              @click="goSaveSpu(row)"
              type="primary"
              :icon="Plus"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="修改" placement="bottom" effect="light">
            <el-button
              @click="showUpdateSpu(row)"
              type="warning"
              :icon="Edit"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="查看" placement="bottom" effect="light">
            <el-button type="" :icon="InfoFilled"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="bottom" effect="light">
            <el-button type="danger" :icon="Delete"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9]"
      :small="true"
      layout=" prev, pager, next, jumper,->,sizes,total"
      :total="30"
      @size-change="getSpuList()"
      @current-change="getSpuList()"
    />
  </el-card>
</template>

<script lang="ts">
export default {
  name: "SpuList",
};
</script>

<script lang="ts" setup>
import { ref, watch } from "vue";
import { getSpuListApi } from "@/api/product/spu";
import type { SpuList } from "@/api/product/model/spuModel";
import { Plus, Edit, Delete, InfoFilled } from "@element-plus/icons-vue";
import { useCategoryStore } from "@/stores/category";
const categoryStore = useCategoryStore();
const currentPage = ref(1);
const pageSize = ref(3);
const total = ref(0);
const spuList = ref<SpuList>([]);
const emit = defineEmits(["showSpuList", "setSpu"]);

const showSaveOrUpdate = () => {
  emit("showSpuList", 2);
  emit("setSpu", {
    spuName: "",
    tmId: undefined,
    description: "",
    spuImageList: [],
    spuSaleAttrList: [],
  });
};

const goSaveSpu = (row) => {
  emit("showSpuList", 3);
  emit("setSpu", row);
};
//请求分页数据
const getSpuList = async () => {
  const res = await getSpuListApi({
    page: currentPage.value,
    limit: pageSize.value,
    category3Id: categoryStore.category3Id as number,
  });
  spuList.value = res.records;
  total.value = res.total;
};

watch(
  () => categoryStore.category3Id,
  (category3Id) => {
    if (!category3Id) {
      spuList.value = [];
      currentPage.value = 1;
      pageSize.value = 3;
      total.value = 0;
      return;
    }
    getSpuList();
  },
  {
    immediate: true,
  }
);
const showUpdateSpu = (row) => {
  console.log(row, "****************");
  emit("showSpuList", 2);
  emit("setSpu", {
    ...row,
    spuImageList: [],
    spuSaleAttrList: [],
  });
};
</script>

<style scoped>
.spu-table {
  margin: 10px 0;
}
</style>
